package com.sangeng.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.sangeng.domain.ResponseResult;
import com.sangeng.domain.dto.TagListDto;
import com.sangeng.domain.entity.Tag;
import com.sangeng.domain.vo.PageVo;
import com.sangeng.domain.vo.TagListVo;
import com.sangeng.domain.vo.TagUpdateVo;
import com.sangeng.enums.AppHttpCodeEnum;
import com.sangeng.mapper.TagMapper;
import com.sangeng.service.TagService;
import com.sangeng.utils.BeanCopyUtils;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import java.util.List;

/**
 * 标签(Tag)表服务实现类
 *
 * @author makejava
 * @since 2023-10-11 10:24:20
 */
@Service("tagService")
public class TagServiceImpl extends ServiceImpl<TagMapper, Tag> implements TagService {
    /**
     * 获取所有的标签列表
     * @param pageNum
     * @param pageSize
     * @param tagListDto
     * @return
     */

    @Override
    public ResponseResult<PageVo> pageTagList(Integer pageNum, Integer pageSize, TagListDto tagListDto) {
        //1.封装查询条件
        LambdaQueryWrapper<Tag> lqw=new LambdaQueryWrapper<>();
        lqw.eq(StringUtils.hasText(tagListDto.getName()), Tag::getName, tagListDto.getName());
        lqw.eq(StringUtils.hasText(tagListDto.getRemark()), Tag::getRemark, tagListDto.getRemark());
        //2.封装分页条件
        Page<Tag> page=new Page(pageNum,pageSize);
        //3.查询数据
        page(page,lqw);
        //4.封装返回数据
        PageVo pageVo=new PageVo(page.getRecords(),page.getTotal());
        return ResponseResult.okResult(pageVo);
    }
    /**
     * 添加标签
     * @param tag
     * @return
     */
    @Override
    public ResponseResult add(Tag tag) {
        return save(tag)?ResponseResult.okResult():ResponseResult.errorResult(AppHttpCodeEnum.ERR);
    }
    /**
     * 根据id删除标签
     * @param id
     * @return
     */
    @Override
    public ResponseResult delete(Long id) {
        return removeById(id)?ResponseResult.okResult():ResponseResult.errorResult(AppHttpCodeEnum.ERR);
    }
    /**
     * 根据id查询数据
     * @param id
     * @return
     */
    @Override
    public ResponseResult<TagUpdateVo> getSingleTag(Long id) {
        Tag tag = getById(id);
        TagUpdateVo tagUpdateVo = BeanCopyUtils.copyBean(tag, TagUpdateVo.class);
        return ResponseResult.okResult(tagUpdateVo);
    }
    /**
     * 根据id修改数据
     * @param tag
     * @return
     */
    @Override
    public ResponseResult update(Tag tag) {
        return updateById(tag)?
                ResponseResult.okResult():ResponseResult.errorResult(AppHttpCodeEnum.ERR);
    }
    /**
     * 获取所有的标签列表
     * @return
     */
    @Override
    public ResponseResult<TagListVo> listAllTag() {
        List<Tag> list = list();
        List<TagListVo> tagListVos = BeanCopyUtils.copyBeanList(list, TagListVo.class);
        return ResponseResult.okResult(tagListVos);
    }

}
